﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;

namespace WinFormsApp1.TongShiWei
{
    public partial class ShowIPQCData : UIForm
    {
        public ShowIPQCData()
        {
            InitializeComponent();
            //给开启状态赋值
            GetIPQCTypeData();
            //显示检测标准表数据
            ShowIPQCData_();
        }
        #region 开启状态
        public async void GetIPQCTypeData()
        {
            List<IPQCStateData> iPQCStateDatas = new List<IPQCStateData>()
            {
                new IPQCStateData{ IPQCStateId=0,IPQCStateName="请选择开启状态"},
                new IPQCStateData{ IPQCStateId=1,IPQCStateName="启动"},
                new IPQCStateData{ IPQCStateId=2,IPQCStateName="禁用"},
                new IPQCStateData{ IPQCStateId=3,IPQCStateName="全开"}
            };
            IPQCType.DataSource = iPQCStateDatas;
            IPQCType.DisplayMember = "IPQCStateName";
            IPQCType.DisplayMember = "IPQCStateId";
        }
        /// <summary>
        /// 定义开启状态类
        /// </summary>
        public class IPQCStateData
        {
            public int IPQCStateId { get; set; }
            public string IPQCStateName { get; set; }
        }
        #endregion
        #region 定义检测类型
        /// <summary>
        /// 定义检测类型数据
        /// </summary>sh
        public async void ShowIPQCTypeData()
        {
            List<IPQCTypeData> iPQCTypeData = new List<IPQCTypeData>
            {
                new IPQCTypeData {IPQCTypeName="请选择检测类型"},
                new IPQCTypeData {IPQCTypeName="进场检测"},
                new IPQCTypeData {IPQCTypeName="宰前检测"},
                new IPQCTypeData {IPQCTypeName="宰后检测"},
                new IPQCTypeData {IPQCTypeName="肉质品质检测"},
                new IPQCTypeData {IPQCTypeName="其他检测"},
            };
            IPQCTypeData_.DataSource = iPQCTypeData;
            IPQCTypeData_.DisplayMember = "IPQCTypeName";
        }

        /// <summary>
        /// 定义检测类型类
        /// </summary>
        public class IPQCTypeData
        {
            public int IPQCTypeId { get; set; }
            public string IPQCTypeName { get; set; }
        }
        #endregion
        #region 显示检测标准表数据
        int AddButton = 0;
        public async void ShowIPQCData_(int pid=0)
        {
            GetIPQC getIPQC = new GetIPQC();
            if (pid == 1)
            {
                getIPQC = new GetIPQC()
                {
                    LiveAnimalsDocumentTIme = LiveAnimelsDocumentTime.Text,
                    IsIPQC = IPQCType.SelectedIndex,
                    IPQCName = IPQCName.Text,
                    IPQCType = IPQCType.SelectedIndex.ToString()
                };
            }
            else
            {
                getIPQC = new GetIPQC()
                {
                    LiveAnimalsDocumentTIme = "",
                    IsIPQC = IPQCType.SelectedIndex,
                    IPQCName = IPQCName.Text,
                    IPQCType = ""
                };
            }
            //http://localhost:5159/api/TSW/ShowIPQCData
            var res = await HttpClientHelper.RequestUrl<GetIPQC, ApiResult<List<IPQC>>>(URLCommon.Url2, "/TSW/ShowIPQCData", HttpClientHelper.MethodType.POST, getIPQC);
            if (res.Code > 0)
            {
                foreach (var item in res.Data)
                {
                    IPQCListData.Rows.Add(item.IPQCId, item.LiveAnimalsDocumentTIme.ToString("yyyy-MM-dd"), item.LiveAnimalsDocument, item.IPQCName, item.IPQCType, item.UseGoodsName, item.IsIPQC == true ? "禁用" : "开启", item.IPQCAuditState == true ? "已审核" : "待审核", item.IPQCCreateName, item.AuditStateTIme.ToString("yyyy-MM-dd"));
                }
                if (AddButton == 0)
                {
                    //禁用
                    DataGridViewButtonColumn GetDataButton = new DataGridViewButtonColumn();
                    GetDataButton.Text = "应用商品";
                    GetDataButton.UseColumnTextForButtonValue = true;
                    //修改
                    DataGridViewButtonColumn UpdateButton = new DataGridViewButtonColumn();
                    UpdateButton.Text = "修改";
                    UpdateButton.UseColumnTextForButtonValue = true;
                    //启动 
                    DataGridViewButtonColumn DeleteButton = new DataGridViewButtonColumn();
                    DeleteButton.Text = "删除";
                    DeleteButton.UseColumnTextForButtonValue = true;
                    IPQCListData.Columns.AddRange(GetDataButton, UpdateButton, DeleteButton);
                    AddButton += 1;
                    //给按钮添加点击事件
                    IPQCListData.CellClick += new DataGridViewCellEventHandler(UpIPQCData);
                }
            }
        }
        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton2_Click(object sender, EventArgs e)
        {
            ShowIPQCData_(1);
        }
        /// <summary>
        /// 表字段
        /// </summary>
        public class IPQC
        {
            /// <summary>
            /// 检疫标准Id
            /// </summary>
            public int IPQCId { get; set; }
            /// <summary>
            /// 单据Id
            /// </summary>
            public string LiveAnimalsDocument { get; set; } = string.Empty;
            /// <summary>
            /// 单据日期
            /// </summary>
            public DateTime LiveAnimalsDocumentTIme { get; set; } = DateTime.Now;
            /// <summary>
            /// 标准名称
            /// </summary>
            public string IPQCName { get; set; } = string.Empty;
            /// <summary>
            /// 检疫类型
            /// </summary>
            public string IPQCType { get; set; } = string.Empty;
            /// <summary>
            /// 执行标准
            /// </summary>
            public string ExecuteIPQC { get; set; } = string.Empty;
            /// <summary>
            /// 应用商品
            /// </summary>
            public string UseGoodsName { get; set; } = string.Empty;
            /// <summary>
            /// 开启状态
            /// </summary>
            public bool IsIPQC { get; set; } = false;
            /// <summary>
            /// 审核状态
            /// </summary>
            public bool IPQCAuditState { get; set; } = false;
            /// <summary>
            /// 制单人
            /// </summary>
            public string IPQCCreateName { get; set; } = string.Empty;
            /// <summary>
            /// 制单时间
            /// </summary>
            public DateTime AuditStateTIme { get; set; } = DateTime.Now;
            /// <summary>
            /// 检疫项目
            /// </summary>
            public string PRQSName { get; set; }
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsDeleteIPQC { get; set; } = false;
        }
        /// <summary>
        /// 查询表字段
        /// </summary>
        public class GetIPQC
        {
            /// <summary>
            /// 单据日期
            /// </summary>
            public string LiveAnimalsDocumentTIme { get; set; }
            /// <summary>
            /// 开启状态
            /// </summary>
            public int IsIPQC { get; set; }
            /// <summary>
            /// 标准名称
            /// </summary>
            public string IPQCName { get; set; } = string.Empty;
            /// <summary>
            /// 检疫类型
            /// </summary>
            public string IPQCType { get; set; } = string.Empty;
        }
        #endregion

        /// <summary>
        /// 显示新增商品按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void uiButton1_Click(object sender, EventArgs e)
        {
            AddIPQCData_ addIPQCData_ = new AddIPQCData_();
            addIPQCData_.Show();
            this.Hide();
        }
         #region 删除+修改+应用商品(按钮)
        public async void UpIPQCData(object sender, DataGridViewCellEventArgs e)
        {
            //http://localhost:5159/api/TSW/DelOrUpData
            var CellNum =e.ColumnIndex;
            var IPQCId = Convert.ToInt32(this.IPQCListData.Rows[e.RowIndex].Cells[0].Value);
            int Num = 0;
            switch (CellNum)
            {
                case 10:
                    AddCommodityData_ addCommodityData = new AddCommodityData_(IPQCId);
                    addCommodityData.Show();
                    break;
                case (11 or 12):
                    DelOrUpContext delOrUpContext = new DelOrUpContext();
                    delOrUpContext.IPQCId = Convert.ToInt32(IPQCListData.Rows[e.RowIndex].Cells[0].Value);
                    if (CellNum == 11)
                    {
                        delOrUpContext.Pid = 2;
                    }
                    else if (CellNum == 12)
                    {
                        delOrUpContext.Pid = 1;
                    }
                    var res = await HttpClientHelper.RequestUrl<DelOrUpContext, ApiResult<DelOrUpContext>>(URLCommon.Url1, "/TSW/DelOrUpData", HttpClientHelper.MethodType.POST, delOrUpContext);
                    if (CellNum == 11 && res.Code > 0)
                    {
                        IPQCListData.Rows.Clear();
                        MessageBox.Show("修改成功");
                        Num = 1;
                    }
                    else if (CellNum == 11)
                    {
                        MessageBox.Show("修改失败");
                    }
                    else if (CellNum == 12 && res.Code > 0)
                    {
                        IPQCListData.Rows.Clear();
                        MessageBox.Show("删除成功");
                        Num = 1;
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                    break;
            };
            if (Num == 1)
            {
                ShowIPQCData_();
            }
        }
        //定义删除修改字段
        public class DelOrUpContext
        { 
            public int IPQCId {  get; set; }
            public int Pid {  get; set; }
        }
        #endregion
    }
}
